<template>
	<view>
		<u-navbar back-icon-color="#ffffff" title="收货地址管理" title-color="#ffffff" :background="background"></u-navbar>
		<view class="content">
			<uni-swipe-action>
				<uni-swipe-action-item :options="options" @click="onClick(index)" @change="change" v-for="(res, index) in addressList" :key="res.id">
					<view class="item">
						<view class="left" @click="choose(res)">
							<view class="top">
								<view class="name">{{ res.receiver }}</view>
								<view class="phone">{{ res.phoneNum }}</view>
								<view class="tag">
									<u-tag v-show="res.defaultSign" class="u-m-l-20" type="success" text="默认" mode="light" size="mini" />
									<!-- <u-tag class="u-m-l-20" type="warning" text="公司" mode="dark" size="mini" /> -->
								</view>
							</view>
							<view class="bottom u-line-2">{{ res.address }}</view>
						</view>
						<view class="right" @click="edit(res.id)">
							<u-icon name="edit-pen" :size="30" color="#999999"></u-icon>
							<text>编辑</text>
						</view>
					</view>
				</uni-swipe-action-item>
			</uni-swipe-action>
		</view>
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>
				新增收货地址
			</view>
		</view>
		<view>
			<u-modal
				width="500"
				v-model="modalShow"
				:content="content"
				:title="title"
				:mask-close-able="true"
				:show-cancel-button="true"
				@confirm="confirmAddress(addressId)"
			></u-modal>
		</view>
		<view><u-toast ref="uToast" /></view>
		<u-modal v-model="modelShow" :show-cancel-button="true" :content="modelContent" @confirm="delAddress(addressId)"></u-modal>
	</view>
</template>

<script>
import uniSwipeAction from '@/components/uni-swipe-action/uni-swipe-action.vue';
import uniSwipeActionItem from '@/components/uni-swipe-action-item/uni-swipe-action-item.vue';
export default {
	components: {
		uniSwipeAction,
		uniSwipeActionItem
	},
	data() {
		return {
			background: {
				//backgroundColor: "#17933f"

				// 导航栏背景图
				// background: 'url(https://cdn.uviewui.com/uview/swiper/1.jpg) no-repeat',
				// 还可以设置背景图size属性
				// backgroundSize: 'cover',

				// 渐变色
				backgroundImage: 'linear-gradient(90deg, rgb(0,164,133), rgb(51,241,205))'
			},
			siteList: [],
			//
			addressList: [],
			addressId: 0,
			//
			modalShow: false,
			content: '',
			title: '',
			//滑动操作
			show: false,
			options: [
				{
					text: '删除',
					style: {
						backgroundColor: '#fa3534'
					}
				}
			],
			//
			modelShow:false,
			modelContent:'确认删除该条地址吗'
		};
	},
	onLoad() {
		try {
			const token = uni.getStorageSync('token');
			if (token) {
				this.userInfo = uni.getStorageSync('userInfo');
				console.log(this.userInfo);
				if(!this.userInfo){
					//#ifdef APP-PLUS
					uni.reLaunch({
						url: '/pages/login/index'
					});
					//#endif
					//#ifdef MP-WEIXIN
					uni.reLaunch({
						url: '/pages/login/xcxLogin'
					});
					//#endif
				}
				this.getAddress(this.userInfo.id);
			} else {
				//#ifdef APP-PLUS
				uni.reLaunch({
					url: '/pages/login/index'
				});
				//#endif
				//#ifdef MP-WEIXIN
				uni.reLaunch({
					url: '/pages/login/xcxLogin'
				});
				//#endif
			}
		} catch (e) {
			// error
		}
	},
	onShow() {
		this.getAddress(this.userInfo.id);
	},
	methods: {
		getAddress(userId) {
			this.$u.api.searchReceiver({ userId: userId }).then(res => {
				console.log('searchReceiver');
				console.log(res);
				if (res.data.content.length == 0) {
					this.addressList = null;
				} else {
					this.addressList = res.data.content;
					console.log(this.addressList);
				}
			});
		},
		//
		choose(res) {
			console.log(res);
			this.addressId = res.id;
			this.modalShow = true;
			this.content = '是否切换当前地址为默认地址';
			this.title = '温馨提示';
		},
		confirmAddress(addressId) {
			console.log(addressId);
			this.$u.api.editReceiver({ id: addressId, defaultSign: true }).then(res => {
				console.log('searchReceiver');
				console.log(res);
				if (res.statusCode == 200) {
					this.$refs.uToast.show({
						title: '收货地址切换成功',
						type: 'success'
					});
					setTimeout(() => {
						this.$u.route({
							type: 'navigateBack'
						});
					}, 1500);
				}else{
					this.$refs.uToast.show({
						title: '切换收货地址失败',
						type: 'error'
					});
				}
			});
		},
		//
		edit(addressId) {
			this.$u.route({
				type: 'to',
				params: {
					addressId: addressId
				},
				url: '/pages/address/addSite'
			});
		},
		toAddSite() {
			if (this.userInfo.realName == false) {
				this.$refs.uToast.show({
					title: '当前用户未认证，无法新增',
					type: 'error'
				});
			} else {
				this.$u.route({
					type: 'to',
					/* params: {
						goodsId: goodsId
					}, */
					url: '/pages/address/addSite'
				});
			}
		},
		//删除地址
		delAddress(addressId) {
			let _t = this;
			console.log(addressId)
			var ids = [];
			ids.push(addressId);
			console.log(ids);
			_t.$u.api.delReceiver(ids).then(res => {
				console.log(res);
				if (res.statusCode == 200) {
					_t.$refs.uToast.show({
						title: '移出购物！',
						type: 'success'
					});
					_t.getAddress(_t.userInfo.id);
				}
			});
		},
		//
		onClick(index) {
			console.log(index)
			let _t = this;
			_t.modelShow = true;
			_t.addressId = _t.addressList[index].id
			//_t.delCar(_t.addressList[e.index].id);
			//_t.addressList.splice(e.index, 1);
			console.log('当前点击的是第' + _t.addressId);
		},
		change(open) {
			console.log('当前开启状态：' + open);
		}
	}
};
</script>

<style lang="scss" scoped>
.content {
}
.item {
	padding: 40rpx 30rpx;
	display: flex;
	justify-content: space-between;
	.left {
		width: 75vw;
		.top {
			display: flex;
			font-weight: bold;
			font-size: 28rpx;
			.phone {
				margin-left: 40rpx;
			}
			.tag {
				display: flex;
				font-weight: normal;
				align-items: center;
				text {
					display: block;
					width: 60rpx;
					height: 34rpx;
					line-height: 34rpx;
					color: #ffffff;
					font-size: 20rpx;
					border-radius: 6rpx;
					text-align: center;
					margin-left: 30rpx;
					background-color: rgb(49, 145, 253);
				}
				.red {
					background-color: red;
				}
			}
		}
		.bottom {
			display: flex;
			margin-top: 20rpx;
			font-size: 28rpx;
			justify-content: space-between;
			color: #999999;
		}
	}

	.right {
		display: flex;
		align-items: center;
		text {
			text-align: center;
			padding-left: 10rpx;
			padding-right: 20rpx;
			color: $u-tips-color;
		}
	}
}
.addSite {
	display: flex;
	justify-content: space-around;
	width: 600rpx;
	line-height: 80rpx;
	position: absolute;
	bottom: 30rpx;
	left: 80rpx;
	background-color: #00a485;
	border-radius: 60rpx;
	font-size: 28rpx;
	.add {
		display: flex;
		align-items: center;
		color: #ffffff;
		.icon {
			margin-right: 10rpx;
		}
	}
}
</style>
